import { getInterpolationFn } from "@/hw1/polynomial";
import { getQuadSplineFn } from "@/hw1/quadratic-spline";
import { IPoint } from "@/models/gui";
import { distance2, IFn2 } from "@/models/math";

export function getChordalFn (points: IPoint[]): { fn: IFn2; start: number, end: number } {
  // t(i+1) - t(i) = || k(i+1) - k(i) ||, 用多项式插值
  const xPoints: IPoint[] = [];
  const yPoints: IPoint[] = [];
  let t = 0;
  points.forEach((p, i) => {
    if (i > 0) {
      t += distance2(p, points[i - 1]);
    }
    xPoints.push({ x: t, y: p.x });
    yPoints.push({ x: t, y: p.y });
  });
  const fx = getInterpolationFn(xPoints);
  const fy = getInterpolationFn(yPoints);

  const fn = (t) => {
    return {
      x: fx(t),
      y: fy(t),
    };
  };
  return {
    fn,
    start: 0,
    end: t
  };
}

export function getChordalQuadSplineFn (points: IPoint[]): { fn: IFn2; start: number, end: number } {
  // t(i+1) - t(i) = || k(i+1) - k(i) ||, 二次样条
  const xPoints: IPoint[] = [];
  const yPoints: IPoint[] = [];
  let t = 0;
  points.forEach((p, i) => {
    if (i > 0) {
      t += distance2(p, points[i - 1]);
    }
    xPoints.push({ x: t, y: p.x });
    yPoints.push({ x: t, y: p.y });
  });
  const fx = getQuadSplineFn(xPoints);
  const fy = getQuadSplineFn(yPoints);

  const fn = (t) => {
    return {
      x: fx(t),
      y: fy(t),
    };
  };
  return {
    fn,
    start: 0,
    end: t
  };
}